package com.example.myblog.controller;

import com.example.myblog.Service.UserService;
import com.example.myblog.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class RegisterController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public String register(@RequestParam String username,
                           @RequestParam String password,
                           @RequestParam String name,
                           @RequestParam String gender,
                           @RequestParam int age,
                           @RequestParam String email,
                           RedirectAttributes redirectAttributes) {
        int result = userService.register(username, password, name, gender, age, email);
        if (result > 0) {
            redirectAttributes.addFlashAttribute("success", "注册成功，请登录！");
            return "redirect:/login";
        } else {
            redirectAttributes.addFlashAttribute("error", "注册失败，可能是账号已存在！");
            return "redirect:/register";
        }
    }
}